package com.unipay.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unipay.common.result.Result;
import com.unipay.common.utils.MD5;
import com.unipay.hosp.service.HospitalSetService;
import com.unipay.model.hosp.HospitalSet;
import com.unipay.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Project: yitong_parent
 * @Author: C.yu
 * @Time: 2023-03-18 18:57:59
 * @Time: 2023-03-18 18:57:59
 * @Desc: TODO
 */
@Api(tags = "Hosp设置首页")
@RestController
@RequestMapping("/unipay/hosp/hospitalSet")
//@CrossOrigin    // ---跨域---
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //1、查询
    @ApiOperation(value = "成员列表信息")
    @GetMapping("/findAll")
    public Result findAll() {
    /*
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new UnipayException(222,  "自定义异常”处理！！！需要手动抛出...");
        }
    */
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        System.out.println(hospitalSetList);
        if (hospitalSetList != null) {
            return Result.ok(hospitalSetList);
        } else {
            return Result.fail(hospitalSetList);
        }
    }

    //2、删除
    @ApiOperation(value = "删除某个成员")
    @DeleteMapping("/removeByID/{id}")
    public Result removeByID(@PathVariable("id") Long id) {
        boolean b = hospitalSetService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3、条件查询---带“分页”
    @ApiOperation(value = "条件分页查询")
    //@GetMapping("/pageQuery/{curPage}/{limit}")
    @PostMapping("/pageQuery/{curPage}/{limit}")
    public Result pageQuery(@PathVariable("curPage") Long curPage,
                            @PathVariable("limit") Long limit,
                            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {      // @RequestBody：通过JSON数据传参，把数据值放到Vo
        //（1）创建page对象，传递“当前页、每页条数”
        Page<HospitalSet> page = new Page<HospitalSet>(curPage, limit);
        //（2）构造条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (hosname != null && hosname != "") {
            queryWrapper.like("hosname", hosname);
        }
        if (hoscode != null && hoscode != "") {
            queryWrapper.eq("hoscode", hoscode);
        }
        //（3）调用service方法
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, queryWrapper);
        return Result.ok(pageHospitalSet);
    }

    //4、添加
    @ApiOperation(value = "添加成员")
    @PostMapping(value = "/save")
    public Result save(@RequestBody HospitalSet hospitalSet) {
        //（1）状态：1 能用、0 不能用
        hospitalSet.setStatus(1);
        //（2）TODO 秘钥，MD5加密，“当前时间 + 随机值” 生成
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok(hospitalSet);
        } else {
            return Result.fail(hospitalSet);
        }
    }

    //5、根据ID获取单个
    @ApiOperation(value = "根据ID获取成员")
    @GetMapping(value = "/getByID/{id}")
    public Result getByID(@PathVariable("id") Long id) {
        //int i = 1 / 0;    -----全局“特定异常”处理----
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet == null) {
            return Result.fail(hospitalSet);
        } else {
            return Result.ok(hospitalSet);
        }
    }

    //6、修改
    @ApiOperation(value = "更新成员")
    @PutMapping(value = "/update")
    public Result updateByID(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7、批量删除
    @ApiOperation(value = "批量删除")
    @DeleteMapping(value = "/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean b = hospitalSetService.removeByIds(idList);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    //8、更新状态：设置 锁定、解锁
    @ApiOperation(value = "更新状态")
    @PutMapping(value = "/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status) {
        int i = hospitalSetService.updateStatus(id, status);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //9、发送：签名秘钥
    @ApiOperation(value = "发送秘钥")
    @PutMapping(value = "/sendKey/{id}")
    public Result updateStatus(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.getSignKey();
        hospitalSet.getHoscode();
        // TODO 发送短信
        return Result.ok(hospitalSet);
    }
}
